package store.controller;

import store.dao.GoodsDAO;
import store.po.Goods;
import store.utils.Pager;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/goods/list")
public class ListGoods extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String name=request.getParameter("name");
        if(name==null) name="";
        try {
            Pager pager = (Pager) request.getAttribute("pager");
            List<Goods> goodsList=new GoodsDAO().findGoods(name,pager);
            request.setAttribute("goodsList",goodsList);
            request.getRequestDispatcher("/goods.jsp").forward(request,response);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e.getMessage());
        }
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
}
